<?php
class Validation_Form extends Zend_Form{
	
	public function __construct(){
		parent::__construct();
		
		$this->setMethod('post');	
			
		$this->addValidationEngineClass();
		foreach($this->getDisplayGroups() as $displayGroup){
			$displayGroup->removeDecorator('DtDdWrapper');
		}
	}
	
	
	public function appendValidationEngineFiles($view){
		
        $view->headScript()->appendFile($view->baseUrl('/js/validation_engine/js/jquery.validationEngine-pl.js'));
        $view->headScript()->appendFile($view->baseUrl('/js/validation_engine/js/jquery.validationEngine.js'));
        $view->headLink()->appendStylesheet($view->baseUrl('/js/validation_engine/css/validationEngine.jquery.css'));

	$view->headScript()->appendFile($view->baseUrl('/js/jquery_ui/js/jquery.ui.datepicker-pl.js'));
        $view->headScript()->appendFile($view->baseUrl('/js/jquery_ui/js/jquery-ui-1.8.16.custom.min.js'));
        $view->headLink()->appendStylesheet($view->baseUrl('/js/jquery_ui/css/jquery-ui-1.8.16.custom.css'));

        $view->headScript()->appendScript('
        $(document).ready(function(){

        	$("form").validationEngine({promptPosition : "centerRight"});
		$("#date").datepicker();
		
       	});
        ');
	}

	public function addValidationEngineClass(){
		foreach($this->getElements() as $element){
			$_classes = array();	
		if ($element->isRequired()) 
			$_classes[]='required';
		if($element->getValidator('StringLength'))
        		$_classes[]='minSize['.$element->getValidator('StringLength')->getMin().']';
		if($element->getValidator('StringLength'))
        		$_classes[]='maxSize['.$element->getValidator('StringLength')->getMax().']';
        	if($element->getValidator('EmailAddress'))
        		$_classes[]='custom[email]';
        	if($element->getValidator('Date'))
        		$_classes[]='custom[date]';	
        		
        	if($element->getValidator('Alpha'))
        		$_classes[]='custom[onlyLetterSp]';
		if($element->getValidator('Identical'))
        		$_classes[]='equals[password]';
        	
		 	$classes='validate[';
	        foreach($_classes as $k=>$class){   	
	        	if($k!=0) $classes.= ',';
	        	$classes.= $class;
	        }
	        $classes.=']';
	        if(!$element->getAttrib('class')) $element->setAttrib('class', $classes);
		}
		
	}
	
	
	
	public function addSubmit($value = 'Wyślij'){
		$this->addElement('submit', $value);
	}
	
	public function getSubmit(){
		return $this->getElement('submit');
	}
	
	public function isPostValid($request){
		if ($request->isPost() && $this->isValid($request->getPost())) return true;
	}
}
?>